<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Opsurat extends CI_Controller {	
	
	public function index()
	{
		if(!$this->session->userdata('name')):
			redirect('auth');
		endif;
		
		$var['title']	= 'Opinion Surat';
		$no = $this->session->userdata('no_opsurat');
		$kl = $this->session->userdata('klien_opsurat');
		$ta = $this->session->userdata('tgl_opsurat_awal');
		$tk = $this->session->userdata('tgl_opsurat_akhir');
		$var['surats']	= $this->opsurats->getData($no,$kl,$ta,$tk);
		$var['content'] = 'index-opsurat';
		$this->load->view('index',$var);
	}
	
	function action(){
		if($this->session->userdata('no_opsurat')==''):
			$surat=array('no_opsurat'=>$this->input->post('no'),'klien_opsurat'=>$this->input->post('s'));
		else:
			$surat=array('no_opsurat'=>'','klien_opsurat'=>'');
		endif;
		$this->session->set_userdata($surat);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function date(){
		$start_date = $this->input->post('yyyy').'-'.$this->input->post('mm').'-'.$this->input->post('dd');
		$end_date   = $this->input->post('yyyy2').'-'.$this->input->post('mm2').'-'.$this->input->post('dd2');
		if($this->session->userdata('tgl_opsurat_awal')==''):
			$surat=array('tgl_opsurat_awal'=>$start_date,'tgl_opsurat_akhir'=>$end_date);
		else:
			$surat=array('tgl_opsurat_awal'=>'','tgl_opsurat_akhir'=>'');
		endif;
		$this->session->set_userdata($surat);
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function excel(){
		//$this->db->use_table('surats');
		$this->db->select('no_op,tanggal,kap,nama_klien,alamat,keterangan,pembuat');
	    // run joins, order by, where, or anything else here
		$this->db->order_by('tanggal','DESC');
	    $query = $this->db->get('opinions');
        to_excel($query,'OP'); // filename is optional, without it, the plugin will default to 'exceloutput'
	}
	
	public function add_op_group()
	{
		
		$var['title']	= 'Add Opinion Surat';
		$var['no']		= $this->opsurats->getLatestPK('no');
		$var['content'] = 'add-op-group';
		$this->load->view('index',$var);
	}
	
	public function add()
	{
		if(!$this->input->post('kap')):
			redirect('opsurat/addgroup/');
		endif;
		
		if($this->input->post('kap')=='Kuningan Jakarta'):
			$cek = '01';
		elseif($this->input->post('kap')=='Sudirman Jakarta'):
			$cek = '02';
		else:
			$cek = '03';
		endif;
		
		$access = array('kap'=>$this->input->post('kap'),'code'=>$cek,'year'=>$this->input->post('year'));
		$this->session->set_userdata($access);
		
		$var['title']	= 'Add Opinion Surat';
		$var['kap']		= $this->session->userdata('kap');
		$var['code']	= $this->session->userdata('code');	
		$var['no']		= $this->opsurats->getLatestPK('no');
		$var['content'] = 'add-opsurat';
		$this->load->view('index',$var);
	}
	
	function save(){
		if($this->input->post('nama_klien')):
			$this->opsurats->getSave();
		endif;
		redirect('opsurat/index/',301);
	}
	
	public function edit($id)
	{
		$var['title']	= 'Edit Opinion Surat';
		$row            = $this->opsurats->getID($id);
		$var['quo']		= $row; 
		$var['no']		= $this->opsurats->getLatestPK('no');
		$var['content'] = 'edit-opsurat';
		$this->load->view('index',$var);
	}
	
	function update(){
		if($this->input->post('keterangan')):
			$this->opsurats->getUpdate();
		endif;
		redirect('opsurat/index/',301);
	}
	
	function remove($id){
		if($id):
			$this->opsurats->getRemove($id);
		endif;
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	public function review()
	{
		if(!$this->session->userdata('name')):
			redirect('auth');
		endif;
		
		$var['title']	= 'OP Review Surat';
		$no = $this->session->userdata('re_no_opsurat');
		$kl = $this->session->userdata('re_klien_opsurat');
		$ta = $this->session->userdata('re_tgl_opsurat_awal');
		$tk = $this->session->userdata('re_tgl_opsurat_akhir');
		$var['surats']	= $this->opsurats->getDataRE($no,$kl,$ta,$tk);
		$var['content'] = 'index-re-opsurat';
		$this->load->view('index',$var);
	}
	
	public function addreview()
	{		
		$var['title']	= 'Add Review Opinion Surat';
		$var['option']  = $this->opsurats->getData();
		$var['content'] = 'add-re-opsurat';
		$this->opsurats->getData();
		$this->load->view('index',$var);
	}
	
	function saveRE(){
		if($this->input->post('id')):
			$this->opsurats->getSaveRE();
		endif;
		redirect('opsurat/review/',301);
	}
	
	public function editRE($id)
	{
		$var['title']	= 'Edit RE Opinion Surat';
		$row            = $this->opsurats->getID($id);
		$var['quo']		= $row; 
		$var['no']		= $this->opsurats->getLatestPK('no');
		$var['content'] = 'edit-opsurat';
		$this->load->view('index',$var);
	}
	
	function updateRE(){
		if($this->input->post('keterangan')):
			$this->opsurats->getUpdate();
		endif;
		redirect('opsurat/index/',301);
	}
	
	function removeRE($id){
		if($id):
			$this->opsurats->getRemoveRE($id);
		endif;
		redirect($this->input->server('HTTP_REFERER'),301);
	}
	
	function re_excel(){
		$this->db->select('no_op,tanggal,kap,nama_klien,alamat,keterangan,pembuat');
		$this->db->order_by('tanggal','DESC');
	    $query = $this->db->get('opinions_rev');
        to_excel($query,'Rev OP'); 
	}
	
	
}
